package com.zhiyou.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhiyou.dao.LesDao;
import com.zhiyou.model.Lessee;

import com.zhiyou.util.DBUtils;
import com.zhiyou.util.PageBean;

public class LesDaoImpl implements LesDao {

	@Override
	public PageBean<Lessee> findAll(int pc,int ps) {
		PageBean<Lessee> pb = new PageBean<>();
		String sq = "select count(*) from lessee "; 
		Object ob[] = {};
		DBUtils db = new DBUtils();
		ResultSet r = db.select(sq, ob);
		pb.setPc(pc);
		pb.setPs(ps);
		int tr = 0;
		if(r==null){
		}else{
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setTr(tr);
		String sql = "select * from lessee limit ?,? ";
		Object obj[] = {(pc-1)*ps ,ps};
		ResultSet rs = db.select(sql, obj);
		List<Lessee> list = new ArrayList<>();
		if(rs == null ){
		}else{
			try {
				while(rs.next()){
					Lessee lessee = new Lessee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getDate(7),rs.getShort(8));
					
					list.add(lessee);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list);
		db.close();
		return pb;
		
		
		
	}

	@Override
	public Lessee findById(int id) {
		String sql = "select * from lessee where a = 0 and l_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		ResultSet rs = db.select(sql, obj);
		if(rs==null){
			db.close();
			return null;
		}
		try {
			while(rs.next()){
				Lessee lessee = new Lessee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getDate(7),rs.getShort(8));
				db.close();
				return lessee;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		return null;
	}

	@Override
	public boolean delete(int id) {
		String sql = "update lessee set a = 1 where a = 0 and l_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num == 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(Object[] obj) {
		String sql = "update lessee set l_name = ?,l_tel = ?,l_sex = ?,l_nativeplace = ?,l_idcard = ? where l_id = ?";
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num == 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean add(Object[] obj) {
		String sql = "insert into lessee values(null,?,?,?,?,?,now(),0)";
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		db.close();
		if(num == 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int[] dels(int[] id) {
		// TODO Auto-generated method stub
		String sql = "update lessee set a = 1 where a = 0 and l_id = ?";
		DBUtils db = new DBUtils();
		int nums[] = db.dels(sql, id);
		db.close();
		return nums;
	}

	@Override
	public PageBean<Lessee> find(String name, String sex, String nativeplace, int pc, int ps) {
		DBUtils db = new DBUtils();
		PageBean<Lessee> pb = new PageBean<>();
		pb.setPc(pc);
		pb.setPs(ps);
		StringBuffer sq = new StringBuffer("select count(*) from lessee where 1=1 ");
		List<Object> list = new ArrayList<>();
		if(name!=null&&!name.trim().isEmpty()){
			sq.append(" and l_name like ? ");
			list.add("%"+name+"%");
		}
		if(sex!=null&&!sex.trim().isEmpty()){
			sq.append(" and l_sex like ? ");
			list.add("%"+sex+"%");
		}
		if(nativeplace!=null&&!nativeplace.trim().isEmpty()){
			sq.append(" and l_nativeplace like ? ");
			list.add("%"+nativeplace+"%");
		}
		int tr = 0;
		ResultSet r = db.select(sq.toString(), list.toArray());
		if(r==null){
			pb.setTr(0);
		}else{ 
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		StringBuffer sql = new StringBuffer(sq.toString().replace("count(*)", "*"));
		sql.append(" limit ?,?");
		list.add((pc-1)*ps);
		list.add(ps);
		List<Lessee> list1 = new ArrayList<>();
		ResultSet rs = db.select(sql.toString(), list.toArray());
		if (rs==null) {
			
		}else{
			try {
				while(rs.next()){
					Lessee lessee = new Lessee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getDate(7),rs.getShort(8));
					list1.add(lessee);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list1);
		db.close();
		return pb;
	}

}
